package com.bnyy.medicalHousekeeper.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.bnyy.common.Constant;
import com.google.gson.Gson;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";
    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate");
        api = WXAPIFactory.createWXAPI(this, Constant.WX.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG, "onNewIntent");
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.e(TAG, "onReq");
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.e(TAG, new Gson().toJson(baseResp));
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.e(TAG, new Gson().toJson(baseResp));
            PayResp payResp = (PayResp) baseResp;

//            EventBus.getDefault().post(new WXPayEvent(baseResp.errCode == 0 ? Constant.EventBusTag.WX.PAY_SUCCESS : Constant.EventBusTag.WX.PAY_FAILED, payResp.prepayId));
            finish();
        } else {
            if (baseResp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
                switch (baseResp.errCode) {
                    case BaseResp.ErrCode.ERR_OK:
                        Log.e("COMMAND_SENDMESSAGE_TO_WX", "分享成功");
                        break;
                    case BaseResp.ErrCode.ERR_USER_CANCEL:
                        Log.e("COMMAND_SENDMESSAGE_TO_WX", "分享取消");
                        break;
                    case BaseResp.ErrCode.ERR_AUTH_DENIED:
                        Log.e("COMMAND_SENDMESSAGE_TO_WX", "分享被拒绝");
                        break;
                    default:
                        Log.e("COMMAND_SENDMESSAGE_TO_WX", "分享返回");
                        break;
                }
            }
        }
    }
}




































